package org.huangrui.juc.source.cf;

import java.util.concurrent.*;

/**
 * @Author hr
 * @Create 2024-04-01 03:07
 */
public class T02_FuturePool {
    //3个任务，目前只有一个线程main来处理，请问耗时多少？
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long startTime = System.currentTimeMillis();
        ExecutorService pool = Executors.newFixedThreadPool(3);
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            //暂停几毫秒
            try { TimeUnit.MILLISECONDS.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
            return "t1 ...over";
        });
        pool.submit(futureTask);
        FutureTask<String> futureTask1 = new FutureTask<>(()->{
            //暂停几毫秒
            try { TimeUnit.MILLISECONDS.sleep(400);} catch (InterruptedException e) {e.printStackTrace();}
            return "t2...over";
        });
        pool.submit(futureTask1);
        //暂停几毫秒
        try { TimeUnit.MILLISECONDS.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}
        System.out.println(futureTask.get());
        System.out.println(futureTask1.get());
        pool.shutdownNow();
        long endTime = System.currentTimeMillis();
        System.out.println("----costTime:"+(endTime - startTime)+"毫秒");
    }

    private static void m1() {
        long startTime = System.currentTimeMillis();
        //暂停几毫秒
        try { TimeUnit.MILLISECONDS.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
        //暂停几毫秒
        try { TimeUnit.MILLISECONDS.sleep(400);} catch (InterruptedException e) {e.printStackTrace();}
        try { TimeUnit.MILLISECONDS.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}
        long endTime = System.currentTimeMillis();
        System.out.println("----costTime:"+(endTime - startTime)+"毫秒");
    }
}
